package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.CarListPopwindowBinding;
import com.sgb.kjwl.databinding.ItemCarinfoListBinding;
import com.sgb.kjwl.databinding.ItemMixCarinfoListBinding;
import com.sgb.kjwl.model.entity.CarInfo;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.MToast;
import com.swgk.core.util.UserOperating;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 王彬臣
 * @date: 2022/1/7
 * @Description: 车辆选择弹框，可多选和单选
 */
public class MixCarListPopWindow extends BasePopWindow implements View.OnClickListener {

    private CarListPopwindowBinding mBinding;
    //
    private CarListAdapter mAdapter;
    private List<CarInfo> mList;

    public MixCarListPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.car_list_popwindow;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mBinding = (CarListPopwindowBinding) holder.getmBinding();
        mBinding.tvTitle.setText("承运车辆");
        initRecycler();
        initEvent();
    }

    private void initRecycler() {
        mList = new ArrayList<>();
        mAdapter = new CarListAdapter(mList);
        mBinding.rvList.setLayoutManager(new LinearLayoutManager(mContext));
        mBinding.rvList.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, mContext.getResources().getDimension(R.dimen.dp0_5), mContext.getResources().getColor(R.color.color_line)));
        mAdapter.bindToRecyclerView(mBinding.rvList);
        mAdapter.setEmptyView(R.layout.layout_null_car);

    }

    private void initEvent() {
        mBinding.setOnClick(this);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            mAdapter.setSelect(position);
        });
    }

    public void setData(List<CarInfo> list) {
        mList.clear();
        if (list != null) {
            mList.addAll(list);
        }
        mAdapter.notifyDataSetChanged();
    }
    public void setSingleSelect(boolean isSingle){
        if(mAdapter!=null){
            mAdapter.setSingleSelect(isSingle);
        }
    }

    @Override
    public void onClick(View v) {
        if (v == mBinding.tvCancel) {
            dissMiss();
        } else if (v == mBinding.tvSure) {
            if (mAdapter.getSelectDataList().size()==0){
                MToast.showToast(mContext,"请选择车辆");
            }
            onResultClick.result(mAdapter.getSelectDataList(), null, null);
            dissMiss();
        }
    }
    private static class CarListAdapter extends BaseQuickAdapter<CarInfo, BaseViewHolder> {
        int select=-1;//默认不选中 2.1.0版本
        public boolean isSingleSelect=true;//是否单选
        List<CarInfo> mSelect=new ArrayList<>();
        SparseBooleanArray checkList=new SparseBooleanArray();
        public void setSingleSelect(boolean singleSelect) {
            isSingleSelect = singleSelect;
        }

        public List<CarInfo> getSelectDataList() {
            return mSelect;
        }

        public void setSelect(int select) {
            this.select = select;
            notifyDataSetChanged();
        }

        public int getSelect() {
            return select;
        }

        public CarListAdapter(@Nullable @org.jetbrains.annotations.Nullable List<CarInfo> data) {
            super(R.layout.item_mix_carinfo_list,data);
        }

        @Override
        protected void convert(BaseViewHolder helper, CarInfo item) {
            ItemMixCarinfoListBinding mBingding= DataBindingUtil.bind(helper.itemView);
            mBingding.setViewModel(item);
            StringBuilder strType=new StringBuilder();
            if(!TextUtils.isEmpty(item.getCarLenName())){
              strType.append(item.getCarLenName()+" ");
            }
            if(!TextUtils.isEmpty(item.getCarTypeName())){
                strType.append(item.getCarTypeName()+" ");
            }
            mBingding.tvCarInfo.setText(strType.toString());

            if(UserOperating.getInstance().isCompany()){
                mBingding.tvCarType.setVisibility(View.VISIBLE);
                //todo 更改车辆类型背景颜色
            if(item.getType()==2){
                mBingding.tvCarType.setText("挂靠车");
                mBingding.tvCarType.setBackgroundResource(R.drawable.bg_price);
            }else{
                mBingding.tvCarType.setText("自营车");
                mBingding.tvCarType.setBackgroundResource(R.drawable.gradient_radius_2_solid_orange);
            }
            }
            if(isSingleSelect) {
                if (select == helper.getLayoutPosition()) {
                    mBingding.ivSelect.setSelected(true);
                } else {
                    mBingding.ivSelect.setSelected(false);
                }
            }else{
                if(select==helper.getLayoutPosition()){
                   if(!mBingding.ivSelect.isSelected()){
                       mBingding.ivSelect.setSelected(true);
                       mSelect.add(item);
                       checkList.put(helper.getLayoutPosition(),true);
                   }else{
                       mBingding.ivSelect.setSelected(false);
                       mSelect.remove(item);
                       checkList.put(helper.getLayoutPosition(),false);
                   }
                   select=-1;//防止车辆列表过多，滑动显示出问题
                }else {
                    if(checkList.size()>0){
                        if(checkList.get(helper.getLayoutPosition())){
                            mBingding.ivSelect.setSelected(true);
                        }else{
                            mBingding.ivSelect.setSelected(false);
                        }
                    }else {
                        mBingding.ivSelect.setSelected(false);
                    }
                }
            }

        }
    }
}
